package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class UploadPage extends Utils {
    // 指定页面的 URL
    private static String url = "http://127.0.0.1:8080/upload.html";
    // 设置显式等待
    private WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
    public UploadPage() {
        super(url);
    }

    // 检查页面正确加载
    public void uploadPageRight() {
        // 查找是否有歌手输入框
        driver.findElement(By.cssSelector("#singerInput"));
        driver.findElement(By.cssSelector("#fileInput"));
        String title = driver.getTitle();
        assert title.equals("上传音乐");
    }

    // 检查上传音乐功能
    public void uploadMusic() throws InterruptedException {
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\李宇春 - 咏春.mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("李宇春");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        assert alert.getText().equals("已经拥有当前歌曲! ");
        alert.accept();

        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\Linkin Park - In The End.mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("Linkin Park");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("音乐上传成功! ");
        alert.accept();
        String title = driver.getTitle();
        assert title.equals("在线音乐列表");
    }

    // 检查返回首页的功能
    public void uploadBack() {
        driver.get(url);
        driver.findElement(By.cssSelector("#uploadFrom > a")).click();
        String title = driver.getTitle();
        assert title.equals("在线音乐列表");
    }

    public void uploadManyMusic() throws InterruptedException {
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\星华 - 烟火人间（完整版）（翻自 添儿呗）.mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("星华");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        assert alert.getText().equals("音乐上传成功! ");
        alert.accept();
        String title = driver.getTitle();
        assert title.equals("在线音乐列表");
//        driver.get(url);
//        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\醉影An 老君师 - 小巷酒馆（Cover 郑国锋   何曼婷）.mp3");
//        driver.findElement(By.cssSelector("#singerInput")).sendKeys("醉影An");
//        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
//        wait.until(ExpectedConditions.alertIsPresent());
//        assert alert.getText().equals("音乐上传成功! ");
//        alert.accept();
//        title = driver.getTitle();
//        assert title.equals("在线音乐列表");
//        driver.get(url);
//        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\谣君 - Way Back Home（Cover：SHAUN）.mp3");
//        driver.findElement(By.cssSelector("#singerInput")).sendKeys("谣君");
//        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
//        wait.until(ExpectedConditions.alertIsPresent());
//        assert alert.getText().equals("音乐上传成功! ");
//        alert.accept();
//        title = driver.getTitle();
//        assert title.equals("在线音乐列表");
//        driver.get(url);
//        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\醉雪 - 无涯（春节版）.mp3");
//        driver.findElement(By.cssSelector("#singerInput")).sendKeys("醉雪");
//        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
//        wait.until(ExpectedConditions.alertIsPresent());
//        assert alert.getText().equals("音乐上传成功! ");
//        alert.accept();
//        title = driver.getTitle();
//        assert title.equals("在线音乐列表");
//        driver.get(url);
//        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\赵京 - 齐天大圣.mp3");
//        driver.findElement(By.cssSelector("#singerInput")).sendKeys("赵京");
//        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
//        wait.until(ExpectedConditions.alertIsPresent());
//        assert alert.getText().equals("音乐上传成功! ");
//        alert.accept();
//        title = driver.getTitle();
//        assert title.equals("在线音乐列表");
//        driver.get(url);
//        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\小忆 - Way Back Home（Cover：SHAUN）.mp3");
//        driver.findElement(By.cssSelector("#singerInput")).sendKeys("小忆");
//        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
//        wait.until(ExpectedConditions.alertIsPresent());
//        assert alert.getText().equals("音乐上传成功! ");
//        alert.accept();
//        title = driver.getTitle();
//        assert title.equals("在线音乐列表");
        driver.get(url);
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\夏初凝 - 落雁罡风.mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("夏初凝");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("音乐上传成功! ");
        alert.accept();
        title = driver.getTitle();
        assert title.equals("在线音乐列表");
        driver.get(url);
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\王者荣耀&KiTheory - Evolve (淬炼) [mqms2].mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("KiTheory");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("音乐上传成功! ");
        alert.accept();
        title = driver.getTitle();
        assert title.equals("在线音乐列表");
        driver.get(url);
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\水源小樱 - 白月光与朱砂痣.mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("水源小樱");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("音乐上传成功! ");
        alert.accept();
        title = driver.getTitle();
        assert title.equals("在线音乐列表");
        driver.get(url);
        driver.findElement(By.cssSelector("#fileInput")).sendKeys("D:\\音乐下载\\晴愔 - 眉间雪(纯歌版).mp3");
        driver.findElement(By.cssSelector("#singerInput")).sendKeys("晴愔");
        driver.findElement(By.cssSelector("#uploadFrom > input[type=submit]:nth-child(3)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        assert alert.getText().equals("音乐上传成功! ");
        alert.accept();
        title = driver.getTitle();
        assert title.equals("在线音乐列表");
    }
}
